# from django.http import HttpResponse
import span as span
from django.shortcuts import render  # 用render将数据填充到模板文件
# from django.contrib import auth
import pymysql

from BiliBiliVideo.spider import  spider2

'''
Django的form的作用：
 1、生成html标签
 2、用来做用户提交的验证
 Form的验证思路
 前端：form表单
后台：创建form类，当请求到来时，先匹配，匹配出正确和错误信息。
'''

def index(request):#党铭均
    return render(request,'index.html')

def regist(request):#蒙樊健
    if request.method=='POST':
        username = request.POST['username']
        password = request.POST['password']
        #连接数据库
        conn = pymysql.connect(
            host="localhost", user="root", password="",
            database="bilibilivideo", port=3306,
            charset='utf8'
        )
        cursor = conn.cursor()
        sql_select = "select * from testmodel_users where uname=%s"
        cursor.execute(sql_select, username)
        info = cursor.fetchone()
        if info is None:
            sql_select = "insert into testmodel_users(uname,password) values (%s,%s)"
            cursor.execute(sql_select, (username,password))
            cursor.close()
            conn.close()
            context = {'results': '注册成功'}
            return render(request, 'login.html', context)
        else:
            context={'results':'用户名已被注册，请更换用户名'}
            return render(request,'regist.html',context)
    return render(request,'regist.html')


def login(request):#党铭均
    #request.session.clear_expired()
    if request.method=='POST':
        username=request.POST.get('username')
        password=request.POST.get('password')
        #print(username,password)
        # re = auth.authenticate(username=username)# 用户认证
        conn = pymysql.connect(
            host="localhost", user="root", password="",
            database="bilibilivideo", port=3306,
            charset='utf8'
        )
        cursor = conn.cursor()
        sql = "select * from testmodel_users where uname=%s"
        cursor.execute(sql,username)
        info=cursor.fetchone()
        cursor.close()
        conn.close()
        if info is not None:
            if info[2]==password:#登陆成功
                request.session['username']=username
                context={'username':username}
                return render(request, 'index2.html',context)

            else:  #数据库里不存在与之对应的数据
                context={'results':'登陆失败'}
                return render(request,'login.html',context)  #登陆失败
        else:
            return render(request,'login.html',{'login_error':'用户不存在'})
    return render(request,'login.html')

def logout(request):#党铭均
    del request.session['username']
    return render(request,'index.html')
def index2(request):#蒙樊健
    return render(request,'index2.html')

def content(request):#王曾辉
    spider=spider2(0)
    notebooks=spider.go()
    spider = notebooks
    username=request.session['username']
    context = {'results': spider,'username':username}
    return render(request,'content.html',context)

def content_next(request):#王曾辉
    spider = spider2(1)
    notebooks = spider.go()
    spider = notebooks
    username = request.session['username']
    context = {'results': spider, 'username': username}
    return render(request, 'content.html', context)

def content_pre(request):#王曾辉
    spider = spider2(-1)
    notebooks = spider.go()
    spider = notebooks
    username = request.session['username']
    context = {'results': spider, 'username': username}
    return render(request, 'content.html', context)
def content_save(request):#张聪
    spider = spider2(2)
    notebooks = spider.go()
    spider = notebooks
    username = request.session['username']
    context = {'results': spider, 'username': username,'save':"存储成功"}
    return render(request, 'content.html', context)
def content_read(request):#张聪
    conn = pymysql.connect(
        host="localhost", user="root", password="",
        database="bilibilivideo", port=3306,
        charset='utf8'
    )
    cursor = conn.cursor()
    sql='select * from spider'
    cursor.execute(sql)
    spider=cursor.fetchall()
    username = request.session['username']
    context = {'results2': spider, 'username': username,'save':"读取成功"}
    return render(request, 'content_read.html', context)
def content_search(request):#魏常品
    conn = pymysql.connect(
        host="localhost", user="root", password="",
        database="bilibilivideo", port=3306,
        charset='utf8'
    )
    r='搜索成功'
    key=request.GET['key']
    cursor = conn.cursor()
    sql = "select * from spider where title like '%%%%%s%%%%' "
    sql=sql%(key)
    cursor.execute(sql)
    spider = cursor.fetchall()
    username = request.session['username']
    if spider is ():
        r='搜索结果为空'
    context = {'results2': spider, 'username': username, 'save':r}
    return render(request,'content_read.html',context)
def content_del(request):#魏常品
    conn = pymysql.connect(
        host="localhost", user="root", password="",
        database="bilibilivideo", port=3306,
        charset='utf8'
    )
    r=''
    cursor = conn.cursor()
    sql = "delete from spider"
    re=cursor.execute(sql)
    sql = 'select * from spider'
    cursor.execute(sql)
    spider = cursor.fetchall()
    username = request.session['username']
    if re is not None:
        r='删除成功'
    context = {'results2': spider, 'username': username, 'save':r}
    return render(request,'content_read.html',context)